import Rating from '../../model/Rating'
import { getEventParam } from '../../utils/utils'

// pages/rating/rating.js
Page({
  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      score: 0,
      content: '',
    },
    rules: [
      {
        name: 'score',
        rules: [{ required: true, message: '请为该服务评分' }],
      },
      {
        name: 'content',
        rules: [
          { required: true, message: '评价内容不能为空' },
          { minlength: 10, message: '评价内容不能少于 10 个字' },
        ],
      },
    ],
    illustration: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const order = JSON.parse(options.order)
    this.setData({
      order,
    })
  },
  handleRating(event) {
    const score = getEventParam(event, 'rating')
    this.setData({
      ['formData.score']: score,
    })
  },
  handleInputChange(event) {
    const value = getEventParam(event, 'value')
    this.setData({
      ['formData.content']: value,
    })
  },
  handleSubmit() {
    this.selectComponent('#form').validate(async (valid, errors) => {
      if (!valid) {
        const errorMsg = errors.map((error) => error.message)
        this.setData({
          error: errorMsg.join(';'),
        })
        return
      }
      wx.showLoading({
        title: '正在提交',
        mask: true,
      })
      const illustration = this.data.illustration.map(item.url)
      await Rating.createRating(
        this.data.order.id,
        this.data.formData.score,
        this.data.formData.content,
        illustration
      )
      wx.hideLoading()
      this.getOpenerEventChannel().emit('rating')
      await wx.showModal({
        title: '提示',
        content: '评价成功,点击确认返回订单详情',
        showCancel: false,
      })
      wx.navigateBack()
    })
  },
  handleUploadSuccess(event) {
    this.data.illustration = getEventParam(event, 'files')
  },
  handleUploadDelete(event) {
    const deleteIndex = this.data.illustration.findIndex(
      (item) => item.key === event.detail.item.key
    )
    this.data.illustration.splice(deleteIndex, 1)
  },
})
